import { defineStore } from 'pinia'
// 导入axios
import request from '@/utils/request'
import { CategoryItem, IApiRes, TopCategoryItem, TopCategory } from '@/types/data'
// 顶级菜单列表资源本地化
import { topCategory } from '../constants'
// 应为只有汉字，所以给它添加一个id属性
const defaultCategory = topCategory.map((item, index) => ({ id: index.toString(), name: item }))
export default defineStore('category', {
    state: () => ({
        // 一级分类列表
        list: defaultCategory as CategoryItem[],
        // 一级分类详情
        topCategory: {} as TopCategory
    }),
    actions: {
        // 获取首页-全部分类（包含推荐商品）
        async getCateGoryList() {
            const { data: { result } } = await request.get<IApiRes<CategoryItem[]>>('/home/category/head')
            this.list = result
        },

        // 获取一级类名的详情数据
        async getTopCateGoryList(id: string) {
            const { data: { result } } = await request.get<IApiRes<TopCategory>>('/category', {
                params: { id }
            })
            this.topCategory = result
        }
    },
    getters: {},
})